<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/23 0023
 * Time: 上午 11:07
 */

namespace Home\Model;
use Think\Model;

class BCWzflModel extends Model{
    protected $info = null;
    public function __construct(){
        parent::__construct();
        $this->info = (S('catinfo'));
    }

    //获取所有的二级分类
    public function getcat(){
        $field = 'flno,flmc,flbm,sjflbm';
        $info = $this->field($field)->where('sjflbm is null and flbm is not null')->select();
        $data = [];
        foreach($info as $k=>$v){
            $_data = $this->field($field)->where("sjflbm={$v['flbm']}")->select();
            if($_data != false){
                $data[] = $_data;
            }
        }
        foreach($data as $k=>$v){
            foreach($v as $k1=>$v1){
                $arr[] = $v1;
            }
        }
        return $arr;
    }

    /**
     * 取出每个二级分类下的最后一级分类
     * @param $flbm  分类编码
     * @return array|bool
     */
    public function getcatlast($flbm){
        $field = 'flno,flmc,flbm,sjflbm';
        $list = []; //未查到的数组
        if(!$info = $this->field($field)->where("sjflbm='{$flbm}'")->select()){ //查第三级
            return false; //查不到返回false
        }
        foreach($info as $k=>$v){
            if(!$arr = $this->field($field)->where("sjflbm='{$v['flbm']}'")->select()){  //查找每个第三级分类的第四级
                $list[] = $info[$k];  //查不到第四级把第三级分类存入数组
            }else{
                $data[] = $arr;
            }
        }
        if(!$data){  //判断如果第四级分类是否存在
            return $list; //不存在返回第三级分类
        }else{
            foreach($data as $k=>$v){  //存在就三维数组转二维
                foreach($v as $k1=>$v1){
                    $row[] = $v1;
                }
            }
        }
        foreach($row as $k=>$v){    //循环第第四级分类
            if(!$wu = $this->field($field)->where("sjflbm='{$v['flbm']}'")->select()){  //查找每个四级分类的第五级
                $list[] = $row[$k];   //查不到第五级把第四级分类存入数组
            }else{
                $last[] = $wu;
            }
        }
        if($last){ //判断第五级分类如果存在
            foreach($last as $k=>$v){  //三维数组转二维
                foreach($v as $k1=>$v1){
                    $_last[] = $v1;
                }
            }
            if($list){ //判断list数组如果存在
                return array_merge($_last,$list); //合并第五级分类的数组和list数组
            }else{
                return $_last;  //判断list数组如果不存在,返回第五级分类的数组
            }
        }elseif(!$last){
            return $list;//判断第五级分类如果不存在 返回$list
        }
//        return array(
//            'row' => $row,
//            'list' => $list,
//            'last' => $last
//        );
    }

    public function getcatwzbm($flno){
        $info = $this->alias('a')->field('a.wzno,a.wzmc,a.dhbs,b.flmc,c.qc')
            ->join('left join b_c_wzfl b on a.wzfl=b.flno')
            ->join('left join b_p_gysjbqk c on a.gysno=c.gysno')
            ->where(array(
                'a.wzno' => array('eq',$flno)
            ))->select();
    }

    /**
     * 根据物资分类ID取出该分类下的筛选条件
     * @param $flno  分类ID
     * @return array|mixed
     */
    public function getwzflwzbmfilter($flno){
        $filter = [];
        //取出该分类的属性筛选数据
        $filter['sx'] = $this->getcat_sx_filter($flno);

        //取出该分类的型号和供应商筛选数据
        $filter['xh_gys'] = $this->getcat_xhgys_filter($flno);

        //dump($filter);exit;

        return $filter;

    }

    /**
     *  取出该分类的属性筛选数据
     * @param $flno  该分类下ID
     * @return array|mixed
     */
    public function getcat_sx_filter($flno){
        //$a = microtime(true);
        $filter = [];
        $sql = "select row.sxno,row.sxmc,row.sxdw,GROUP_CONCAT(row.sxz) sxz from
                (select DISTINCT b.zdysxz sxz ,b.sxno,c.sxmc,c.sxdw from b_c_wzbm a
                join b_c_zdysx b on a.wzno=b.wzno
                join b_c_sxgz c on b.sxno=c.sxno
                where wzfl={$flno}) as row
                group by row.sxno";
        //echo $sql ;exit;
        $info = D()->query($sql);
        foreach ($info as $k => &$v) {
            $v['sxz'] = array_unique(explode(',', $v['sxz']));
            if (count($v['sxz']) < 2) {    //判断去重之后的属性值如果少于2个,就不做筛选条件
                unset($info[$k]);
            }
        }
        //dump(microtime(true)-$a);
        //dump($info);exit;
        $filter['sx'] = $info;
        $redis = linkredis();
        //把筛选数据缓存到redis
        $redis->set($flno . '_fl_wzbmfilter', serialize($filter));
        $redis->expire($flno . '_fl_wzbmfilter', 3600 * 48);
        return $info;
    }

    /**
     * 取出该分类的型号和供应商筛选数据
     * @param $flno
     * @return array
     */
    public function getcat_xhgys_filter($flno){
        //$a = microtime(true);
        $redis = linkredis();
        $filter = unserialize($redis->get($flno.'_fl_wzbmfilter'));
        //xh型号
        $data = [];
        $xh_sql = "select DISTINCT xh xh from b_c_wzbm where wzfl={$flno}";
        $xhinfo = D()->query($xh_sql);
        $xhinfo = array_column($xhinfo,'xh');
        if (count($xhinfo) > 1) {
            $data['xh'] = $xhinfo;
        }

        //供应商
        $gys_sql = "select DISTINCT a.gysno gys , b.qc from b_c_wzbm a
                    join b_p_gysjbqk b on a.gysno=b.gysno
                    where wzfl={$flno}";
        $gysinfo = D()->query($gys_sql);
        if (count($gysinfo) > 1) {
            $data['gys'] = $gysinfo;
        }
        $filter['xh_gys'] = $data;
        //dump(microtime(true)-$a);
        //dump($data);
        //把筛选数据缓存到redis
        $redis->set($flno . '_fl_wzbmfilter', serialize($filter));
        $redis->expire($flno . '_fl_wzbmfilter', 3600 * 48);
        return $data;
    }

    //根据提交的条件取出筛选数据
    public function get_tj_catfilter($flno,$xh_gys,$sx){
        $b_c_zdysx = D('b_c_zdysx');
        if($xh_gys && $sx){
            //先取出型号和供应商筛选的数据,如果为空,直接返回空数组
            $xh_gys_wznos = $this->xh_gys($xh_gys,$flno);
            //dump($xh_gys_wznos);exit;
            if(!$xh_gys_wznos){
                return array();
            }else{
                //取出属性筛选的数据,如果有值,就和型号取出的数据求并集,如果没值,返回型号取出的数据
                $sx_wznos = $b_c_zdysx->filter_sx($sx,$flno,'wzfl');
                //dump($sx_wznos);exit;
                if($sx_wznos){
                    $wznos = array_intersect($xh_gys_wznos,$sx_wznos);
                }else{
                    $wznos = $xh_gys_wznos;
                }
            }
            //dump($wznos);exit;

            return $wznos;
        }elseif($sx){
            $wznos = $b_c_zdysx->filter_sx($sx,$flno,'wzfl');
            //dump($wznos);exit;
            return $wznos;
        }elseif($xh_gys){
            $info = $this->xh_gys($xh_gys,$flno);
            return $info;
        }

    }


    /**根据型号和供应商筛选数据
     * @param $xh_gys
     * @param $flno
     * @return mixed
     */
    public function xh_gys($xh_gys,$flno){
        $str = '';  //初始化型号和供应商where条件拼接
        foreach($xh_gys as $k=>&$v){
            $a = "'".implode("','",$v)."'";
            $str .= $k ." in (". $a .") and "; //拼接条件
        }
        $str = rtrim($str ,' and');
        $where = "wzfl={$flno} and ".$str;
        //echo $where;exit;
        //根据型号和供应商筛选的条件取出所有的物资ID
        $sql = "select wzno from b_c_wzbm where $where";

        $info = D()->query($sql);
        $info = array_column($info,'wzno');
        //dump($info);exit;
        return $info;
    }

    public function get_wzfl_wzbm_filter($wznos,$length=50){
        $tiaoshu = I('post.offset'); //获取条数
        $offset = $tiaoshu + $length;
        $wznos  = array_values($wznos);
        $arr = [];
        if (!$tiaoshu) {
            foreach($wznos as $k=>$v){
                if($k < $length){
                    $arr[] = $v;
                }
            }

        } else {
            foreach ($wznos as $k => $v) {
                if ($k >= $tiaoshu && $k < $offset) {
                    $arr[] = $v;
                }
            }
        }
        $arr = implode(',',$arr);
        //dump($arr);exit;
        $sql = "select a.wzno,a.wzmc,a.dhbs,b.qc from b_c_wzbm a 
                left join b_p_gysjbqk b on a.gysno=b.gysno 
                where wzno in ($arr)";
        $info = D()->query($sql);
        foreach($info as $k=>&$v){
            $type = explode(' ',$v['dhbs']);
            $v['dhbs'] = array($type[0],$type[1],$type[2]);
        }
        return array(
            'info' => $info,
            'offset' => $offset
        );
    }

}